package org.jcms.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jcms.model.Customer;
import org.jcms.uitils.CookieUtil;
/**
 * 参数：type:0 向购物车中加增加购书，其它参数名：bookIsbn,addNumber
 *     type:1 删除购物车中的指定记录，其它参数名：bookIsbn
 *     type:2 清空购物车
 *     type:3 预留功能，修改指定书的订购数量，其它参数名：bookIsbn，addNumber
 * @author Administrator
 *
 */
public class CustomerShoppingCartManage extends HttpServlet {


	private static final long serialVersionUID = 1L;


	public CustomerShoppingCartManage() {
		super();
	}


	public void destroy() {
		super.destroy(); 
		
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String retJson = "{\"error\":@err,\"msg\":\"@msg\"}";
		String err = "1";
		String msg = "";
		Customer customer = (Customer) request.getSession().getAttribute("customer");
		String cookieName = "bookTrolley";
		String cookiePath = request.getContextPath() + "/customer";
		String type = request.getParameter("type");
		String bookIsbn = request.getParameter("bookIsbn");
		String addNumber = request.getParameter("addNumber");
		try {
			if(customer==null){
				msg = "网面已过期";
			}else{
				 cookieName = "bookTrolley"+customer.getCustomerId();
				 int intType = Integer.parseInt(type);
					switch (intType) {
					case 0:
						if (bookIsbn == null || "".equals(bookIsbn)) {
							msg = "教材ISBN不能为空";
						} else {
							if (addNumber == null || "".equals(addNumber)
									|| !addNumber.matches("0*[1-9]\\d*")) {
								msg = "订购教材的数量不合法";
							} else {
								List<Map<String, String>> json = new ArrayList<Map<String, String>>();
								HashMap<String, String> map = new HashMap<String, String>();
								map.put("bookIsbn", bookIsbn);
								map.put("orderBookNumber", addNumber);
								json.add(map);
								int n = CookieUtil.add(cookieName, cookiePath,
										"bookIsbn", "orderBookNumber", json,
										3600 * 24 * 30, request, response);
								msg = "" + n;
								err = "0";
							}
						}
						break;
					case 1:
						if (bookIsbn == null || "".equals(bookIsbn)) {
							msg = "教材ISBN不能为空";
						}else{
							CookieUtil.delete(cookieName, cookiePath, "bookIsbn", bookIsbn, 3600 * 24 * 30, request, response);
							err="0";
							msg="教材("+bookIsbn+")已从购物车中删除";
						}
						break;
					case 2:
						CookieUtil.dropCookie(cookieName, cookiePath, request, response);
						err="0";
						msg="购物车已清空";
						break;
					case 3:
						if (bookIsbn == null || "".equals(bookIsbn)) {
							msg = "教材ISBN不能为空";
						} else {
							if (addNumber == null || "".equals(addNumber)
									|| !addNumber.matches("0*[1-9]\\d*")) {
								msg = "订购教材的数量不合法";
							} else {
								List<Map<String, String>> json = new ArrayList<Map<String, String>>();
								HashMap<String, String> map = new HashMap<String, String>();
								map.put("bookIsbn", bookIsbn);
								map.put("orderBookNumber", addNumber);
								json.add(map);
								CookieUtil.set(cookieName, cookiePath, "bookIsbn", json, 3600 * 24 * 30, request, response);
								msg = "修改成功";
								err = "0";
							}
						}
						break;
					default:
						msg = "type错误";
						break;
					}
			}
			
		} catch (Exception e) {
			msg = "系统出错";
		} finally {
			retJson = retJson.replace("@err", err);
			retJson = retJson.replace("@msg", msg);
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}
	}

	public void init() throws ServletException {
		
	}

}
